﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OCR.SystemThread
{
    class TestLocalInfoThread
    {
        public static bool SetFinalInfo_Flowrec = false;
        public static Thread SetFinalInfoFlow_Thread = null;
        public static int iSetFinalInfo_Step = 0;

        public static bool SetFinalInfo_Stop = false;
        public static void SetFinalInfo_Thread()
        {
            SetFinalInfo_Flowrec = true;
            SetFinalInfoFlow_Thread = new Thread(SetFinalInfoFlowThread);
            SetFinalInfoFlow_Thread.IsBackground = true;
            SetFinalInfoFlow_Thread.Start();
        }
        private static void SetFinalInfoFlowThread(object obj)
        {
            while (SetFinalInfo_Flowrec)
            {
                switch (iSetFinalInfo_Step)
                {
                    case 0:
                        Console.WriteLine("iSetFinalInfo_Step----------");
                        iSetFinalInfo_Step = 20;
                        break;
                    case 20:
                        Console.WriteLine("iSetFinalInfo_Step20----------");

                        break;
                    case 5:
                        Bitmap finalBit_NG = (Bitmap)OCR_Form.finalBit.Clone();
                        DateTime dt_NG_Set_Info = DateTime.Now;
                        if (OCR_Form.oCR_Form.InfoTB_01.InvokeRequired)
                        {
                            OCR_Form.oCR_Form.InfoTB_01.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_01.Text = string.Empty));
                        }
                        else
                        {
                            OCR_Form.oCR_Form.InfoTB_01.Text = string.Empty;
                        }

                        if (OCR_Form.oCR_Form.InfoTB_02.InvokeRequired)
                        {
                            OCR_Form.oCR_Form.InfoTB_02.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_02.Text = string.Empty));
                        }
                        else
                        {
                            OCR_Form.oCR_Form.InfoTB_02.Text = string.Empty;
                        }

                        if (OCR_Form.oCR_Form.InfoTB_03.InvokeRequired)
                        {
                            OCR_Form.oCR_Form.InfoTB_03.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_03.Text = string.Empty));
                        }
                        else
                        {
                            OCR_Form.oCR_Form.InfoTB_03.Text = string.Empty;
                        }

                        if (OCR_Form.oCR_Form.InfoTB_04.InvokeRequired)
                        {
                            OCR_Form.oCR_Form.InfoTB_04.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_04.Text = string.Empty));
                        }
                        else
                        {
                            OCR_Form.oCR_Form.InfoTB_04.Text = string.Empty;
                        }

                        if (OCR_Form.oCR_Form.InfoTB_05.InvokeRequired)
                        {
                            OCR_Form.oCR_Form.InfoTB_05.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_05.Text = string.Empty));
                        }
                        else
                        {
                            OCR_Form.oCR_Form.InfoTB_05.Text = string.Empty;
                        }

                        if (OCR_Form.oCR_Form.InfoTB_06.InvokeRequired)
                        {
                            OCR_Form.oCR_Form.InfoTB_06.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_06.Text = string.Empty));
                        }
                        else
                        {
                            OCR_Form.oCR_Form.InfoTB_06.Text = string.Empty;
                        }

                        if (AlgorithmModule.currConfigs.Count >= 1)
                        {
                            if (OCR_Form.oCR_Form.InfoTB_01.InvokeRequired)
                            {
                                OCR_Form.oCR_Form.InfoTB_01.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_01.Text = AlgorithmModule.currConfigs[0].text));
                            }
                            else
                            {
                                OCR_Form.oCR_Form.InfoTB_01.Text = AlgorithmModule.currConfigs[0].text; ;
                            }
                        }
                        if (AlgorithmModule.currConfigs.Count >= 2)
                        {
                            if (OCR_Form.oCR_Form.InfoTB_02.InvokeRequired)
                            {
                                OCR_Form.oCR_Form.InfoTB_02.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_02.Text = AlgorithmModule.currConfigs[1].text));
                            }
                            else
                            {
                                OCR_Form.oCR_Form.InfoTB_02.Text = AlgorithmModule.currConfigs[1].text; ;
                            }
                        }
                        if (AlgorithmModule.currConfigs.Count >= 3)
                        {
                            if (OCR_Form.oCR_Form.InfoTB_03.InvokeRequired)
                            {
                                OCR_Form.oCR_Form.InfoTB_03.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_03.Text = AlgorithmModule.currConfigs[2].text));
                            }
                            else
                            {
                                OCR_Form.oCR_Form.InfoTB_03.Text = AlgorithmModule.currConfigs[2].text; ;
                            }
                        }
                        if (AlgorithmModule.currConfigs.Count >= 4)
                        {
                            if (OCR_Form.oCR_Form.InfoTB_04.InvokeRequired)
                            {
                                OCR_Form.oCR_Form.InfoTB_04.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_04.Text = AlgorithmModule.currConfigs[3].text));
                            }
                            else
                            {
                                OCR_Form.oCR_Form.InfoTB_04.Text = AlgorithmModule.currConfigs[3].text; ;
                            }
                        }
                        if (AlgorithmModule.currConfigs.Count >= 5)
                        {
                            if (OCR_Form.oCR_Form.InfoTB_05.InvokeRequired)
                            {
                                OCR_Form.oCR_Form.InfoTB_05.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_05.Text = AlgorithmModule.currConfigs[4].text));
                            }
                            else
                            {
                                OCR_Form.oCR_Form.InfoTB_05.Text = AlgorithmModule.currConfigs[4].text; ;
                            }
                        }
                        if (AlgorithmModule.currConfigs.Count >= 6)
                        {
                            if (OCR_Form.oCR_Form.InfoTB_06.InvokeRequired)
                            {
                                OCR_Form.oCR_Form.InfoTB_06.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_06.Text = AlgorithmModule.currConfigs[5].text));
                            }
                            else
                            {
                                OCR_Form.oCR_Form.InfoTB_06.Text = AlgorithmModule.currConfigs[5].text; ;
                            }
                        }

                        DateTime dt_NG_isImageResult = DateTime.Now;
                        if (!File.Exists("ImageResult.txt"))
                        {
                            File.WriteAllText("ImageResult.txt", null, Encoding.UTF8);
                        }

                        DateTime dt_NG_MainRect = DateTime.Now;
                        OCR_Form.oCR_Form.mainCameraPb.Image = AlgorithmModule.RectPoints(finalBit_NG, AlgorithmModule.pointsList);

                        DateTime dt_NG_MainResult = DateTime.Now;
                        OCR_Form.oCR_Form.tbNGResult1.Text = "NG";
                        DateTime dt_NG_Txt = DateTime.Now;
                        try
                        {
                            StreamWriter sw = File.AppendText("ImageResult.txt");
                            string timeResult_ng = DateTime.Now.ToString("NG_GPU_记录时间：" + "yyyy:MM:dd:HH:mm:ss.fffffff")
                                + "  算法时长：" + (OCR_Form.dt2 - OCR_Form.dt1).TotalMilliseconds
                                + "  克隆相机图片时长：" + (OCR_Form.dt2_Clone - OCR_Form.dt2).TotalMilliseconds
                                + "  模板比较时长：" + (OCR_Form.dt_CompareResult - OCR_Form.dt2_Clone).TotalMilliseconds
                                + "  主界面设置信息时长：" + (dt_NG_isImageResult - dt_NG_Set_Info).TotalMilliseconds
                                + "  无日志创建日志时长：" + (dt_NG_MainRect - dt_NG_isImageResult).TotalMilliseconds
                                + "  画框显示时长：" + (dt_NG_MainResult - dt_NG_MainRect).TotalMilliseconds
                                + "  显示NG结果时长：" + (dt_NG_Txt - dt_NG_MainResult).TotalMilliseconds
                                + "  GPU抓到图时间：" + OCR_Form.imgDateTime.ToString("yyyy:MM:dd:HH:mm:ss:ffff")
                                + "  总时长：" + (OCR_Form.SendNgDt - OCR_Form.dt1).TotalMilliseconds;
                            Console.WriteLine(timeResult_ng);
                            sw.WriteLine(timeResult_ng);
                            sw.Close();
                        }
                        catch (Exception e)
                        {

                            Console.WriteLine(e.Message);
                        }
                        DateTime dt_NG_Txt_End = DateTime.Now;
                        Console.WriteLine("写日志时长：" + (dt_NG_Txt_End - dt_NG_Txt).TotalMilliseconds);
                        //NG存图
                        Console.WriteLine("GPU-NG存图");
                        try
                        {
                            string outImgPath = Application.StartupPath + "\\out\\";
                            string tempPath = outImgPath + DateTime.Now.ToString("yyyy-MM-dd") + "\\";
                            if (!Directory.Exists(tempPath))
                            {
                                Directory.CreateDirectory(tempPath);
                            }
                            Console.WriteLine("存图路径: " + tempPath);

                            finalBit_NG.Save(tempPath + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "_NG" + ".bmp");
                            AlgorithmModule.RectPoints(finalBit_NG, AlgorithmModule.pointsList).Save(tempPath + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "_NG_A" + ".bmp");
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }

                        iSetFinalInfo_Step = 0;

                        break;
                    case 10:
                        Bitmap finalBit_OK = (Bitmap)OCR_Form.finalBit.Clone();
                        DateTime dt_OK_Set_Info = DateTime.Now;
                        if (OCR_Form.oCR_Form.InfoTB_01.InvokeRequired)
                        {
                            OCR_Form.oCR_Form.InfoTB_01.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_01.Text = string.Empty));
                        }
                        else
                        {
                            OCR_Form.oCR_Form.InfoTB_01.Text = string.Empty;
                        }

                        if (OCR_Form.oCR_Form.InfoTB_02.InvokeRequired)
                        {
                            OCR_Form.oCR_Form.InfoTB_02.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_02.Text = string.Empty));
                        }
                        else
                        {
                            OCR_Form.oCR_Form.InfoTB_02.Text = string.Empty;
                        }

                        if (OCR_Form.oCR_Form.InfoTB_03.InvokeRequired)
                        {
                            OCR_Form.oCR_Form.InfoTB_03.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_03.Text = string.Empty));
                        }
                        else
                        {
                            OCR_Form.oCR_Form.InfoTB_03.Text = string.Empty;
                        }

                        if (OCR_Form.oCR_Form.InfoTB_04.InvokeRequired)
                        {
                            OCR_Form.oCR_Form.InfoTB_04.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_04.Text = string.Empty));
                        }
                        else
                        {
                            OCR_Form.oCR_Form.InfoTB_04.Text = string.Empty;
                        }

                        if (OCR_Form.oCR_Form.InfoTB_05.InvokeRequired)
                        {
                            OCR_Form.oCR_Form.InfoTB_05.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_05.Text = string.Empty));
                        }
                        else
                        {
                            OCR_Form.oCR_Form.InfoTB_05.Text = string.Empty;
                        }

                        if (OCR_Form.oCR_Form.InfoTB_06.InvokeRequired)
                        {
                            OCR_Form.oCR_Form.InfoTB_06.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_06.Text = string.Empty));
                        }
                        else
                        {
                            OCR_Form.oCR_Form.InfoTB_06.Text = string.Empty;
                        }

                        if (AlgorithmModule.currConfigs.Count >= 1)
                        {
                            if (OCR_Form.oCR_Form.InfoTB_01.InvokeRequired)
                            {
                                OCR_Form.oCR_Form.InfoTB_01.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_01.Text = AlgorithmModule.currConfigs[0].text));
                            }
                            else
                            {
                                OCR_Form.oCR_Form.InfoTB_01.Text = AlgorithmModule.currConfigs[0].text; ;
                            }
                        }
                        if (AlgorithmModule.currConfigs.Count >= 2)
                        {
                            if (OCR_Form.oCR_Form.InfoTB_02.InvokeRequired)
                            {
                                OCR_Form.oCR_Form.InfoTB_02.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_02.Text = AlgorithmModule.currConfigs[1].text));
                            }
                            else
                            {
                                OCR_Form.oCR_Form.InfoTB_02.Text = AlgorithmModule.currConfigs[1].text; ;
                            }
                        }
                        if (AlgorithmModule.currConfigs.Count >= 3)
                        {
                            if (OCR_Form.oCR_Form.InfoTB_03.InvokeRequired)
                            {
                                OCR_Form.oCR_Form.InfoTB_03.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_03.Text = AlgorithmModule.currConfigs[2].text));
                            }
                            else
                            {
                                OCR_Form.oCR_Form.InfoTB_03.Text = AlgorithmModule.currConfigs[2].text; ;
                            }
                        }
                        if (AlgorithmModule.currConfigs.Count >= 4)
                        {
                            if (OCR_Form.oCR_Form.InfoTB_04.InvokeRequired)
                            {
                                OCR_Form.oCR_Form.InfoTB_04.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_04.Text = AlgorithmModule.currConfigs[3].text));
                            }
                            else
                            {
                                OCR_Form.oCR_Form.InfoTB_04.Text = AlgorithmModule.currConfigs[3].text; ;
                            }
                        }
                        if (AlgorithmModule.currConfigs.Count >= 5)
                        {
                            if (OCR_Form.oCR_Form.InfoTB_05.InvokeRequired)
                            {
                                OCR_Form.oCR_Form.InfoTB_05.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_05.Text = AlgorithmModule.currConfigs[4].text));
                            }
                            else
                            {
                                OCR_Form.oCR_Form.InfoTB_05.Text = AlgorithmModule.currConfigs[4].text; ;
                            }
                        }
                        if (AlgorithmModule.currConfigs.Count >= 6)
                        {
                            if (OCR_Form.oCR_Form.InfoTB_06.InvokeRequired)
                            {
                                OCR_Form.oCR_Form.InfoTB_06.BeginInvoke(new Action(() => OCR_Form.oCR_Form.InfoTB_06.Text = AlgorithmModule.currConfigs[5].text));
                            }
                            else
                            {
                                OCR_Form.oCR_Form.InfoTB_06.Text = AlgorithmModule.currConfigs[5].text; ;
                            }
                        }

                        DateTime dt_OK_isImageResult = DateTime.Now;
                        if (!File.Exists("ImageResult.txt"))
                        {
                            File.WriteAllText("ImageResult.txt", null, Encoding.UTF8);
                        }

                        DateTime dt_OK_MainRect = DateTime.Now;
                        OCR_Form.oCR_Form.mainCameraPb.Image = AlgorithmModule.RectPoints(finalBit_OK, AlgorithmModule.pointsList);

                        DateTime dt_OK_MainResult = DateTime.Now;
                        OCR_Form.oCR_Form.tbNGResult1.Text = "OK";

                        DateTime dt_OK_Txt = DateTime.Now;
                        try
                        {
                            StreamWriter sw = File.AppendText("ImageResult.txt");
                            string timeResult_ng = DateTime.Now.ToString("OK_GPU_记录时间：" + "yyyy:MM:dd:HH:mm:ss.fffffff")
                                + "  算法时长：" + (OCR_Form.dt2 - OCR_Form.dt1).TotalMilliseconds
                                + "  克隆相机图片时长：" + (OCR_Form.dt2_Clone - OCR_Form.dt2).TotalMilliseconds
                                + "  模板比较时长：" + (OCR_Form.dt_CompareResult - OCR_Form.dt2_Clone).TotalMilliseconds
                                + "  主界面设置信息时长：" + (dt_OK_isImageResult - dt_OK_Set_Info).TotalMilliseconds
                                + "  无日志创建日志时长：" + (dt_OK_MainRect - dt_OK_isImageResult).TotalMilliseconds
                                + "  画框显示时长：" + (dt_OK_MainResult - dt_OK_MainRect).TotalMilliseconds
                                + "  显示OK结果时长：" + (dt_OK_Txt - dt_OK_MainResult).TotalMilliseconds
                                + "  GPU抓到图时间：" + OCR_Form.imgDateTime.ToString("yyyy:MM:dd:HH:mm:ss:ffff")
                                + "  总时长：" + (OCR_Form.SendNgDt - OCR_Form.dt1).TotalMilliseconds;
                            Console.WriteLine(timeResult_ng);
                            sw.WriteLine(timeResult_ng);
                            sw.Close();
                        }
                        catch (Exception e)
                        {

                            Console.WriteLine(e.Message);
                        }
                        DateTime dt_OK_Txt_End = DateTime.Now;
                        Console.WriteLine("写日志时长：" + (dt_OK_Txt_End - dt_OK_Txt).TotalMilliseconds);


                        if (OCR_Form.oCR_Form.saveImgCb_PLC.Checked || OCR_Form.oCR_Form.saveOKImgcB.Checked)
                        {
                            //OK存图
                            Console.WriteLine("GPU-OK存图");
                            try
                            {
                                string outImgPath = Application.StartupPath + "\\out\\";
                                string tempPath = outImgPath + DateTime.Now.ToString("yyyy-MM-dd") + "\\";
                                if (!Directory.Exists(tempPath))
                                {
                                    Directory.CreateDirectory(tempPath);
                                }
                                Console.WriteLine("存图路径: " + tempPath);

                                finalBit_OK.Save(tempPath + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "_OK" + ".bmp");
                                AlgorithmModule.RectPoints(finalBit_OK, AlgorithmModule.pointsList).Save(tempPath + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "_OK_A" + ".bmp");
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                            }
                        }

                        iSetFinalInfo_Step = 0;

                        break;
                }
            }
        }
    }
}
